﻿/***************************************************************************
 * 文件名：ProjSync
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：07.04 周四 09:16:27
 * 创建人：杨洋
 * 邮箱： 2873349982@qq.com
 * =========================================================================
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ProjChangeSync
{
    public interface IProjSync
    {
        /// <summary>
        /// 读取变更
        /// </summary>
        /// <returns></returns>
        List<Changes> ReadChanges();
        /// <summary>
        /// 信息同步
        /// </summary>
        /// <returns></returns>
        Response ProjSync(List<Changes> changes);
        /// <summary>
        /// 同步完向中间库回写
        /// </summary>
        /// <returns></returns>
        Response MiddleChange();
    }

    public class ProjSyncService : IProjSync
    {
        public Response MiddleChange()
        {
            return new Response();
        }

        public Response ProjSync(List<Changes> changes)
        {
            return new Response();
        }

        public List<Changes> ReadChanges()
        {
            return new List<Changes>();
        }
    }

    public class ProjSyncMgr
    {
        public void ProjSync()
        {
            using (SqlConnection conn = new SqlConnection("db"))
            {
                conn.Open();

                // 开启事务
                using (SqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        var service = new ProjSyncService();

                        var list = service.ReadChanges();

                        var response = service.ProjSync(list);
                        if (response.Code != 0)
                        {
                            throw new Exception(response.Message);
                        }

                        var response1 = service.MiddleChange();
                        if (response1.Code != 0)
                        {
                            throw new Exception(response.Message);
                        }

                        // 提交事务
                        tran.Commit();
                        Console.WriteLine("同步完成。");
                    }
                    catch (Exception ex)
                    {
                        // 出现异常，回滚事务
                        tran.Rollback();
                        Console.WriteLine($"事务回滚：{ex.Message}");
                    }
                }
            }
        }
    }
    /// <summary>
    /// 变更信息
    /// </summary>
    public class Changes
    {
        
    }

    public class Response
    {
        /// <summary>
        /// 应答码（0：同步成功 -1：同步失败）
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string Message { get; set; }
    }
}
